package cn.xinfei.xdecision.common.model.component.decisiontree.vo;

import cn.xinfei.xdecision.common.model.component.IgnoredParamCheck;
import cn.xinfei.xdecision.common.model.component.decisiontree.DecisionTreeDetail;
import cn.xinfei.xdecision.common.model.component.decisiontree.DecisionTreeVersion;
import cn.xinfei.xdecision.common.model.component.strategyout.StrategyOutput;
import cn.xinfei.xdecision.common.utils.enums.enginex.ComponentVersionStatusEnum;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Data
@AllArgsConstructor
@NoArgsConstructor
public class DecisionTreeVersionVo extends DecisionTreeVersion {
    private List<StrategyOutput> strategyOutputList;

    @ApiModelProperty("决策树详情列表")
    private List<DecisionTreeDetail> detailList;

    /**
     * @see ComponentVersionStatusEnum
     */
    @ApiModelProperty("版本状态描述")
    @IgnoredParamCheck
    private String statusDesc;

    @IgnoredParamCheck
    private String versionNoDesc;

    public void recursionNodeType() {
        if (!CollectionUtils.isEmpty(this.detailList)) {
            this.detailList.forEach(this::recursionNodeType);
        }
    }

    public void recursionNodeType(DecisionTreeDetail decisionTreeDetail) {
        List<DecisionTreeDetail> childrenDetails = decisionTreeDetail.getChildren();
        if (CollectionUtils.isEmpty(childrenDetails)) {
            decisionTreeDetail.setNodeType(2);
        } else {
            decisionTreeDetail.setNodeType(1);
            childrenDetails.forEach(this::recursionNodeType);
        }
    }

    public void recurrenceSetIdIsNull() {
        if (CollectionUtils.isNotEmpty(this.detailList)) {
            this.detailList.forEach(this::recurrenceDetail);
        }
        if (CollectionUtils.isNotEmpty(this.strategyOutputList)) {
            this.strategyOutputList.forEach(x -> this.setId(null));
        }
        this.setId(null);
    }

    private void recurrenceDetail(DecisionTreeDetail decisionTreeDetail) {
        if (CollectionUtils.isNotEmpty(decisionTreeDetail.getConditionList())) {
            decisionTreeDetail.getConditionList().forEach(x -> {
                x.setId(null);
                x.setDetailId(null);
            });
        }
        decisionTreeDetail.setId(null);
        List<DecisionTreeDetail> children = decisionTreeDetail.getChildren();
        if (CollectionUtils.isNotEmpty(children)) {
            children.forEach(x -> {
                x.setId(null);
                x.setVersionId(null);
                recurrenceDetail(x);
            });
        }
    }
}
